package small.common.designPattern.behavioral.chainOfResponsibility.slotchain;

/**
 * 链式执行器
 */
public abstract class AbstractLinkedProcessorSlot<T> implements ProcessorSlot<T> {

    private AbstractLinkedProcessorSlot<?> next = null;

    @Override
    public void fireEntry(Object resource)throws Throwable {
        if (next != null) {
            next.transformEntry(resource);
        }
    }

    @SuppressWarnings("unchecked")
    void transformEntry(Object resource)throws Throwable {
        T res = (T) resource;
        entry(res);
    }

    @Override
    public void fireExit(Object resource)throws Throwable {
        if (next != null) {
            next.transformExit(resource);
        }
    }

    @SuppressWarnings("unchecked")
    void transformExit(Object resource)throws Throwable {
        T res = (T) resource;
        exit(res);
    }


    /**
     * 获取下一个执行链
     *
     * @return
     */
    public AbstractLinkedProcessorSlot<?> getNext() {
        return next;
    }

    /**
     * 设置下一个执行器链
     *
     * @param next
     */
    public void setNext(AbstractLinkedProcessorSlot<?> next) {
        this.next = next;
    }
}
